﻿using Adp;
using Microsoft.AspNetCore.Mvc;
using Yqd.Models;
using System.Linq;
using Yqd.Controllers.Filter;
using Adp.Common;
using System;
using Yqd.Core;

namespace Yqd.Controllers
{
    [Area("Yqd")]
    public class WxSpecialStandardController : Controller
    {
        YqdContext ctx;

        public WxSpecialStandardController(YqdContext ctx)
        {
            this.ctx = ctx;
        }

        [User]
        public ActionResult GetList()
        {
            var TaskID = Form.GetInt("TaskID");
            var r = ctx.Database.SqlQuery<Yqd_SpecialStandard>("select * from Yqd_SpecialStandard where TaskID={0} order by Tactic asc", TaskID);
            return this.ToJson(new
            {
                success = true,
                totalcount = r.Count(),
                data = r.Select(it => new
                {
                    it.StandardID,
                    it.Content
                })
            });
        }


        /// <summary>
        /// 添加督查标准
        /// </summary>
        /// <returns></returns>
        [User]
        public IActionResult Add(Yqd_SpecialStandard model)
        {
            model.Tactic = ctx.Database.MaxID("Yqd_SpecialStandard", "Tactic");
            model.Instime = DateTime.Now;
            model.EmployeeCode = WxUser.LogonInfo.EmployeeCode;
            model.UnitCode = WxUser.LogonInfo.UnitCode;
            ctx.Yqd_SpecialStandard.Add(model);
            var ret = ctx.SaveChanges();
            return this.ToJson(new
            {
                success = true,
                model.StandardID,
                ret
            });
        }

        /// <summary>
        /// 修改督查标准
        /// </summary>
        /// <returns></returns>
        [User]
        public IActionResult Edit(Yqd_SpecialStandard model)
        {
            var standard = ctx.Yqd_SpecialStandard.FirstOrDefault(it => it.StandardID == model.StandardID);
            if (standard != null)
            {
                standard.Content = model.Content;
                standard.Remark = model.Remark;
            }
            var ret = ctx.SaveChanges();
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }

        [User]
        public ActionResult Delete()
        {
            var StandardID = Form.GetInt("StandardID");
            var s = ctx.Yqd_SpecialStandard.FirstOrDefault(it => it.StandardID == StandardID);
            var ret = ctx.Database.ExecuteSql("delete from Yqd_SpecialStandard where StandardID={0};", StandardID);
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }
    }
}
